package app

import (
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/astaxie/beego/validation"

	"github.com/EDDYCJY/go-gin-example/pkg/logging"
)

// MarkErrors logs error logs
func MarkErrors(errors []*validation.Error) {
	for _, err := range errors {
		logging.Info(err.Key, err.Message)
	}

	return
}

// MarkErrors logs error logs
func HttPPOst(url string, data string) (string, error) {

	method := "POST"

	payload := strings.NewReader(data)

	client := &http.Client{
		Timeout: 180 * time.Second,
	}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		return "", err
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	res, err := client.Do(req)
	if err != nil {
		//fmt.Println(err)
		return "", err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		//fmt.Println(err)
		return "", err
	}
	return string(body), nil
}
